package timing.ukulele.third.controller.weixin;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.third.data.weixin.WxUserTagDTO;
import timing.ukulele.third.persistent.weixin.WxUser;
import timing.ukulele.third.service.weixin.WxUserService;
import timing.ukulele.third.config.mp.WxMpConfiguration;
import java.util.List;

/**
 * 微信用户标签
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/wxUserTag")
public class WxUserTagsController {

	private final WxUserService wxUserService;

	/**
	 * /wxUserTag/saveWxUserTag
	 * @return
	 */
	@Operation(summary = "新增微信用户标签")
	@PostMapping("/saveWxUserTag")
	public ResponseData<WxUserTag> saveWxUserTag(@RequestBody WxUserTagDTO wxUserTagDTO) {
		WxMpUserTagService wxMpUserTagService = WxMpConfiguration.getMpServiceByAppId(wxUserTagDTO.getAppId()).getUserTagService();
		try {
            return ResponseData.success(wxMpUserTagService.tagCreate(wxUserTagDTO.getName()));
		} catch (WxErrorException e) {
            return ResponseData.error("新增微信用户标签失败" + e.getMessage());
		}
	}

	/**
	 * /wxUserTag/updateWxUserTag
	 * @return
	 */
	@Operation(summary = "修改微信用户标签")
	@PutMapping("/updateWxUserTag")
	public ResponseData<Boolean> updateById(@RequestBody WxUserTagDTO wxUserTagDTO) {
		WxMpUserTagService wxMpUserTagService = WxMpConfiguration.getMpServiceByAppId(wxUserTagDTO.getAppId()).getUserTagService();
		try {
            return ResponseData.success(wxMpUserTagService.tagUpdate(wxUserTagDTO.getId(),wxUserTagDTO.getName()));
		} catch (WxErrorException e) {
            return ResponseData.error("修改微信用户标签失败" + e.getMessage());
		}
	}

    /**
     * /wxUserTag/deleteWxUserTag
     * @param id
     * @param appId
     * @return
     */
	@Operation(summary = "删除微信用户标签")
	@DeleteMapping("/deleteWxUserTag")
	public ResponseData<Boolean> deleteWxUserTag(@RequestParam("id") Long id, @RequestParam("appId") String appId) {
		long count = wxUserService.count(Wrappers.<WxUser>lambdaQuery()
				.eq(WxUser::getAppId, appId)
				.and(wrapper -> wrapper
						.eq(WxUser::getTagIdList,"["+id+"]")
						.or()
						.like(WxUser::getTagIdList,","+id+",")
						.or()
						.likeRight(WxUser::getTagIdList,"["+id+",")
						.or()
						.likeLeft(WxUser::getTagIdList,","+id+"]")));
		if(count>0){
            return ResponseData.error("该标签下有用户存在，无法删除");
		}
		WxMpUserTagService wxMpUserTagService = WxMpConfiguration.getMpServiceByAppId(appId).getUserTagService();
		try {
			return  ResponseData.success(wxMpUserTagService.tagDelete(id));
		} catch (WxErrorException e) {
            return ResponseData.error("删除微信用户标签失败" + e.getMessage());
		}
	}

    /**
     * /wxUserTag/allList
     * @param appId
     * @return
     */
    @Operation(summary = "获取微信用户标签全部列表")
    @GetMapping("/allList")
    public ResponseData<List<WxUserTag>> getWxUserList(@RequestParam("appId") String appId) {
        try {
            WxMpUserTagService wxMpUserTagService = WxMpConfiguration.getMpServiceByAppId(appId).getUserTagService();
            List<WxUserTag> listWxUserTag =  wxMpUserTagService.tagGet();
            return ResponseData.success(listWxUserTag);
        } catch (WxErrorException e) {
            return ResponseData.error("获取微信用户标签失败" + e.getMessage());
        }
    }

}
